﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentModel;

namespace DocumentBiz
{
    public class DocumentTypeBiz
    {
        public int SaveItem(DocumentType item)
        {
            var dbContext = new Context();

            dbContext.DocumentTypes.Add(item);
            int newId = dbContext.SaveChanges();

            return newId;
        }

        public void UpdateItem(DocumentType item)
        {
            var dbContext = new Context();
            dbContext.Entry(item).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public List<DocumentType> GetAll()
        {
            var dbContext = new Context();
            return (from item in dbContext.DocumentTypes
                    orderby item.Name ascending
                    select item).ToList();
        }

        public DocumentType GetItemById(int id)
        {
            var dbContext = new Context();
            return (from item in dbContext.DocumentTypes
                    where item.Id == id
                    select item).First();
        }

        public void RemoveItem(List<DocumentType> items)
        {
            var dbContext = new Context();
            foreach (var type in items)
            {
                dbContext.DocumentTypes.Attach(type);
                dbContext.DocumentTypes.Remove(type);
                dbContext.Entry(type).State = EntityState.Deleted;
            }

            dbContext.SaveChanges();
        }
    }
}
